﻿using System;
using l1cs.server.model.Instance;
using System.Collections.Generic;
using System.Xml;

namespace l1cs.server.model.npc.action
{
    public class L1NpcListedAction : L1NpcXmlAction
    {
        private List<L1NpcAction> _actions;

        public L1NpcListedAction() { }

        public L1NpcListedAction( XmlReader reader )
            : base( reader )
        {
            _actions = L1NpcXmlParser.listActions( reader );
        }

        public void addAction( L1NpcAction action )
        {
            _actions.Add( action );
        }

        override public L1NpcHtml execute( String actionName, L1PcInstance pc, L1Object obj, byte[] args )
        {
            L1NpcHtml result = null;
            foreach ( L1NpcAction action in _actions )
            {
                if ( !action.acceptsRequest( actionName, pc, obj ) )
                {
                    continue;
                }
                L1NpcHtml r = action.execute( actionName, pc, obj, args );
                if ( r != null )
                {
                    result = r;
                }
            }
            return result;
        }

        //@Override
        public L1NpcHtml executeWithAmount( String actionName, L1PcInstance pc,
                L1Object obj, int amount )
        {
            L1NpcHtml result = null;
            foreach ( L1NpcAction action in _actions )
            {
                if ( !action.acceptsRequest( actionName, pc, obj ) )
                {
                    continue;
                }
                L1NpcHtml r = action.executeWithAmount( actionName, pc, obj, amount );
                if ( r != null )
                {
                    result = r;
                }
            }
            return result;
        }
    }
}